home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- '''Class for caching TLS sessions.'''
- import thread
- import time
-
- class SessionCache:
- '''This class is used by the server to cache TLS sessions.
-
- Caching sessions allows the client to use TLS session resumption
- and avoid the expense of a full handshake. To use this class,
- simply pass a SessionCache instance into the server handshake
- function.
-
- This class is thread-safe.
- '''
-
- def __init__(self, maxEntries = 10000, maxAge = 14400):
- '''Create a new SessionCache.
-
- @type maxEntries: int
- @param maxEntries: The maximum size of the cache. When this
- limit is reached, the oldest sessions will be deleted as
- necessary to make room for new ones. The default is 10000.
-
- @type maxAge: int
- @param maxAge: The number of seconds before a session expires
- from the cache. The default is 14400 (i.e. 4 hours).'''
- self.lock = thread.allocate_lock()
- self.entriesDict = { }
- self.entriesList = [
- (None, None)] * maxEntries
- self.firstIndex = 0
- self.lastIndex = 0
- self.maxAge = maxAge
-
-
- def __getitem__(self, sessionID):
- self.lock.acquire()
-
- try:
- self._purge()
- session = self.entriesDict[sessionID]
- if session.valid():
- return session
- raise KeyError()
- finally:
- self.lock.release()
-
-
-
- def __setitem__(self, sessionID, session):
- self.lock.acquire()
-
- try:
- self.entriesDict[sessionID] = session
- self.entriesList[self.lastIndex] = (sessionID, time.time())
- self.lastIndex = (self.lastIndex + 1) % len(self.entriesList)
- if self.lastIndex == self.firstIndex:
- del self.entriesDict[self.entriesList[self.firstIndex][0]]
- self.firstIndex = (self.firstIndex + 1) % len(self.entriesList)
- finally:
- self.lock.release()
-
-
-
- def _purge(self):
- currentTime = time.time()
- index = self.firstIndex
- while index != self.lastIndex:
- if currentTime - self.entriesList[index][1] > self.maxAge:
- del self.entriesDict[self.entriesList[index][0]]
- index = (index + 1) % len(self.entriesList)
- continue
- break
- self.firstIndex = index
-
-
-
- def _test():
- import doctest
- import SessionCache
- return doctest.testmod(SessionCache)
-
- if __name__ == '__main__':
- _test()
-
-